﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using cn.jpush.api;
using cn.jpush.api.push.mode;
using cn.jpush.api.push.notification;

namespace Hangjing.SQLServerDAL
{
    /// <summary>
    /// 极光推送
    /// </summary>
    public  class jPushHelper
    {
        public static String app_key = CacheHelper.GetSetValue(12);
        public static String master_secret = CacheHelper.GetSetValue(13);

        static JPushClient client = new JPushClient(app_key, master_secret);

        /// <summary>
        /// 发给个人
        /// </summary>
        /// <param name="deliverid"></param>
        /// <param name="ordercount"></param>
        public static void PushToDeliver(int deliverid,int ordercount)
        {
            if (ordercount <= 0)
            {
                return;
            }

            string msgbody = "您有新订单";
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.ios();
            pushPayload.audience = Audience.s_alias(deliverid+"");

            //pushPayload.audience = Audience.all();

            var notification = new Notification();
            notification.IosNotification = new IosNotification().setAlert(msgbody).setSound("personalOrder.caf").AddExtra("newordercount", "" + ordercount).AddExtra("msgtype", "" + 2);//.setContentAvailable(true);
            //notification.IosNotification = new IosNotification().setAlert(msgbody).setSound("default").AddExtra("newordercount", "" + ordercount).AddExtra("msgtype", "" + 2);

            pushPayload.notification = notification;
            pushPayload.message = Message.content(msgbody);

            if (CacheHelper.GetSetValue(6) == "1")
            {
                //Hangjing.AppLog.AppLog.Info("apns_production" + true + " \r\n master_secret" + master_secret);

                pushPayload.options.apns_production = true;
            }


           

            try
            {
                var result = client.SendPush(pushPayload);
                Hangjing.AppLog.AppLog.Info("发送成功：deliverid=" + deliverid + " \r\n isResultOK=" + (result.isResultOK().ToString())+"msg_id="+result.msg_id);

            }
            catch (Exception e)
            {
                Hangjing.AppLog.AppLog.Error("发送失败：deliverid=" + deliverid + " \r\n " + e);
            }
        }

        /// <summary>
        /// 发给群主
        /// </summary>
        /// <param name="deliverid"></param>
        /// <param name="ordercount"></param>
        public static void PushToGroup(int gid)
        {
            string msgbody = "您有群组订单";
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.ios();
            pushPayload.audience = Audience.s_tag("gid"+gid);
            var notification = new Notification();
            notification.IosNotification = new IosNotification().setAlert(msgbody).setSound("groupOrder.caf").AddExtra("msgtype", "" + 0).AddExtra("orderid", ""); ;

            pushPayload.notification = notification;
            pushPayload.message = Message.content(msgbody);
            if (CacheHelper.GetSetValue(6) == "1")
            {
                pushPayload.options.apns_production = true;
            }

            try
            {
                var result = client.SendPush(pushPayload);

                //Hangjing.AppLog.AppLog.Error("PushToGroup发送成功：gid=" + gid + " \r\n isResultOK=" + (result.isResultOK().ToString()) + "msg_id=" + result.msg_id);

            }
            catch (Exception e)
            {
                Hangjing.AppLog.AppLog.Error("PushToGroup发送失败：gid=" + gid + " \r\n " + e);
            }
        }

        /// <summary>
        /// 发给群主
        /// </summary>
        /// <param name="deliverid"></param>
        /// <param name="ordercount"></param>
        public static void PushToGroup(string dids)
        {
            string msgbody = "您有群组订单";
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.ios();
            pushPayload.audience = Audience.s_alias(dids.Split(','));
            var notification = new Notification();
            notification.IosNotification = new IosNotification().setAlert(msgbody).setSound("groupOrder.caf").AddExtra("msgtype", "" + 0).AddExtra("orderid", "");

            pushPayload.notification = notification;
            pushPayload.message = Message.content(msgbody);
            if (CacheHelper.GetSetValue(6) == "1")
            {
                pushPayload.options.apns_production = true;
            }

            try
            {
                var result = client.SendPush(pushPayload);

                //Hangjing.AppLog.AppLog.Error("PushToGroup发送成功：deliverid=" + dids + " \r\n isResultOK=" + (result.isResultOK().ToString()) + "msg_id=" + result.msg_id);

            }
            catch (Exception e)
            {
                Hangjing.AppLog.AppLog.Error("PushToGroup发送失败：deliverid=" + dids + " \r\n " + e);
            }
        }

        /// <summary>
        /// 发给城市
        /// </summary>
        /// <param name="deliverid"></param>
        /// <param name="ordercount"></param>
        public static void PushToCity(int cityid)
        {
            string msgbody = "您有群组订单";
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.ios();
            pushPayload.audience = Audience.s_tag("cityid" + cityid);
            var notification = new Notification();
            notification.IosNotification = new IosNotification().setAlert(msgbody).setSound("groupOrder.caf").AddExtra("msgtype", "" + 0).AddExtra("orderid", "");

            pushPayload.notification = notification;
            pushPayload.message = Message.content(msgbody);
            if (CacheHelper.GetSetValue(6) == "1")
            {
                pushPayload.options.apns_production = true;
            }

            try
            {
                var result = client.SendPush(pushPayload);

                //Hangjing.AppLog.AppLog.Error("PushToGroup发送成功：deliverid=" + cityid + " \r\n isResultOK=" + (result.isResultOK().ToString()) + "msg_id=" + result.msg_id);

            }
            catch (Exception e)
            {
                Hangjing.AppLog.AppLog.Error("PushToGroup发送失败：deliverid=" + cityid + " \r\n " + e);
            }
        }


        /// <summary>
        /// 发给个人普通消息
        /// </summary>
        /// <param name="deliverid"></param>
        /// <param name="ordercount"></param>
        public static void PushToDeliverMsg(int deliverid, string message, string orderid)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.ios();
            pushPayload.audience = Audience.s_alias(deliverid + "");
            var notification = new Notification();
            notification.IosNotification = new IosNotification().setAlert(message).setSound("default").AddExtra("msgtype", "" + 1).AddExtra("orderid", orderid);

            pushPayload.notification = notification;
            pushPayload.message = Message.content(message);
            if (CacheHelper.GetSetValue(6) == "1")
            {
                pushPayload.options.apns_production = true;
            }

            try
            {
                var result = client.SendPush(pushPayload);

                //Hangjing.AppLog.AppLog.Error("PushToDeliverMsg发送成功：deliverid=" + deliverid + " \r\n isResultOK=" + (result.isResultOK().ToString()) + "msg_id=" + result.msg_id);

            }
            catch (Exception e)
            {
                Hangjing.AppLog.AppLog.Error("PushToDeliverMsg发送失败：deliverid=" + deliverid + " \r\n " + e);
            }
        }


    }
}
